﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Speech.Recognition;

namespace Kokomo.Speech
{
	public class SpeechBindingCollection : ObservableCollection<SpeechBinding>
	{
		protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			base.OnCollectionChanged(e);

			this.InvalidateGrammar();
		}

		protected void InvalidateGrammar()
		{
			this._grammar = null;
		}

		private Grammar _grammar;
		public Grammar GetGrammar()
		{
			if (this._grammar != null) return this._grammar;

			Choices choices=new Choices();
			foreach (var binding in this)
			{
				string phrase = binding.Phrase;
				if (!string.IsNullOrWhiteSpace(phrase))
				{
					choices.Add(phrase);
				}
			}

			GrammarBuilder builder = choices.ToGrammarBuilder();
			Grammar grammar = new Grammar(builder);

			this._grammar = grammar;

			return grammar;
		}
	}
}
